var appInstance = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    shop_name:'',
    cartEdit:0,
    cart_list:[],

    goods_total:0,
    order_total:0,
    all_checked:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


    this.setData({
      shop_name: appInstance.globalData.shop_name
    });

    //todo 获取购物车商品列表
    this.get_cart_list();

    //todo 计算购物车总价值
    // this.cart_caculate();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.get_cart_list();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var p = new Promise( this.get_cart_list );
    p.then(function( result ){
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  get_cart_list:function( resolve ){

    var url = appInstance.globalData.api_path+'&r=cart.index';

    var t = this;

    appInstance.request( url, {}, 'GET', function( res ){

      if(  Array.isArray( res.data.list ) ){
        res.data.list.forEach(function( item ){

          item.checked = 1;

        });

        t.setData({

          cart_list:res.data.list

        },function(){
          t.cart_caculate();
        });
      }
      if( typeof resolve == 'function'){
        resolve( res.data );
      }

    });

  },


  cart_caculate:function(){

    var t = this;
    var cart_ids= '';

    console.log( this.data.cart_list );
    this.data.cart_list.forEach(function( item ){

      if( item.checked ){
        cart_ids += item.id + ',';
      }


    });

    cart_ids = cart_ids.replace(/\,$/,'');
    var url=appInstance.globalData.api_path+'&r=cart.calculate';

    var data = {
      cart_ids:cart_ids
    };
    appInstance.request(url, data, 'GET', res => {

      console.log( res );

      t.setData({
        goods_total:res.data.goods_total,
        order_total:res.data.order_total
      });

    });

  },

  // 修改商品数量
  change_num:function( event ){

    var t = this;


    var dataset = event.target.dataset;
    var product = dataset.product;

    if( dataset.type == '-' ){
      product.goods_num --;
    }else{
      product.goods_num ++;
    }
    product.goods_num  = product.goods_num <= 0 ? 1 : product.goods_num;
    this.data.cart_list.forEach(function( item, index ){

      if( item.id == product.id ){
        t.data.cart_list[index].goods_num = product.goods_num;

      }
    });

    // todo 刷新数据
    this.setData({
      cart_list:this.data.cart_list
    });

    // todo 发送修改请求

    var url = appInstance.globalData.api_path+'&r=cart.change';

    var data = {
      cart_id:product.id,
      number:product.goods_num
    };

    appInstance.request(url, data, 'GET', res => {

      t.cart_caculate();

    });

  },

  edit_cart:function( e ){

    console.log( e );

    this.setData({
      cartEdit:parseInt( e.target.dataset.edit )
    });

  },

  cart_remove:function( e ){
    console.log( e );

    var t = this;
    var url = appInstance.globalData.api_path+'&r=cart.remove';

    var item = e.target.dataset.item;
    var data = {
      cart_id:item.id
    };


    appInstance.request(url, data, {}, res=>{
      t.get_cart_list();
    });


  },


  checkboxChange:function( e ){

    var t = this;
    var checkedArr = e.detail.value;
    var all_checked = 1;
    this.data.cart_list.forEach(function( item, index ){

      if( checkedArr.indexOf( item.id )> -1 ){
        item.checked = 1;
      }else{
        item.checked = 0;
        all_checked = 0;
      }

      // item.checked = allChecked;

    });

    this.setData({

      cart_list:this.data.cart_list,
      all_checked:all_checked
    },function(){
      t.cart_caculate();
    });

  },
  allChange:function( e ){

    var t = this;

    var allChecked = 0;
    if( e.detail.value.indexOf('check-all') > -1 ){
      allChecked = 1;
    }

    this.data.cart_list.forEach(function( item, index ){
      item.checked = allChecked;
    });

    this.setData({
      cart_list:this.data.cart_list
    },function(){

      t.cart_caculate();
    });


  },


  // 进入下单页面
  checkout:function(){

    var cartids = [];
    this.data.cart_list.forEach(function( item ){
      if( item.checked ){
        cartids.push( item.id );
      }
    });


    wx.navigateTo({
      url:'/pages/checkout/checkout?cartids=' + cartids.join(',')
    });

  }



})